package com.zhongfl.crm.lixiao.bean.resolve;

/**
 * @author wang.yuchuan
 * @date 2018/6/24
 */
public class ModelResolverHelper<T, U> {

    private ModelResolver<T, U> resolver;

    public static ModelResolverHelper create(ModelResolver modelResolver) {
        ModelResolverHelper helper = new ModelResolverHelper();
        helper.resolver = modelResolver;
        return helper;
    }

    public static ModelResolverHelper create(Class<?> cls) {
        if (!cls.isAssignableFrom(ModelResolver.class)) {
            throw new IllegalArgumentException("参数必须为ModelResolver的子类");
        }
        try {
            ModelResolver modelResolver = (ModelResolver) cls.newInstance();
            ModelResolverHelper helper = new ModelResolverHelper();
            helper.resolver = modelResolver;
            return helper;
        } catch (InstantiationException e) {
            throw new IllegalArgumentException("参数必须为ModelResolver的子类");
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException("参数必须为ModelResolver的子类");
        }

    }

    public T resolve(U obj) {
        return this.resolver.resolve(obj);
    }
}
